{include file="index/_meta" /}
    <title>核销</title>
</head>
<body>
<div id="app">
    <van-nav-bar
            title="核销"
            :left-text="lefttext"
            left-arrow
            @click-left="jumpUser"
    ></van-nav-bar>

    <van-pull-refresh v-model="isLoading">

        <van-button v-if=" info == '' "
                    @click="scanQRCode"
                    block
                    type="info"
                    style="width: 10rem;height: 10rem;border-radius: 10rem;font-size: 2rem;margin: 5rem auto 2rem auto;">
            扫一扫
        </van-button>

        <div v-else>
            <div class="list">
                <van-cell-group>
                    <van-cell v-if="info.shop != undefined" title="" value="" is-link
                              @click="$router.push({path:'/shop/detail',query:{shop_id:info.shop_id}})">
                        <template #title>
                            <h3 style="margin: 0;">{{info.shop.title}}</h3>
                        </template>
                    </van-cell>
                    <van-cell title="" value="" v-if="info.buy_mode != 'SHOPBUY'" is-link
                              @click="$router.push({path:'/goods_qiang/detail',query:{goods_qiang_id:info.goods_qiang_id}})">
                        <template #title>
                            <div style="width: 20%;float: left;overflow: hidden;border-radius: 5px;">
                                <img v-if="info.goods != undefined" :src="info.goods.img_thumb"
                                     style="width: 100%;">
                            </div>
                            <div style="margin-left: 10px;float: left">
                                <h5 style="margin: 0;">{{info.goods_title}}</h5>
                                <p style="margin: 0;font-size: 12px;">购买数量：{{info.num}}</p>
                            </div>
                        </template>
                    </van-cell>
                    <van-cell title="单价" v-if="info.buy_mode != 'SHOPBUY'">
                        <template #default>
                            {{info.price}}元
                        </template>
                    </van-cell>
                    <van-cell title="总价">
                        <template #default>
                            {{info.total_price}}元
                        </template>
                    </van-cell>
                </van-cell-group>

                <van-cell-group style="margin-top: 10px;">

                    <van-cell>
                        <template #title>
                            <span style="color: #aaa;">联系人姓名:</span><span>{{info.buyer_name}}</span>
                        </template>
                        <template #default>
                            <span style="color: #ff8917">{{info.order_status_text}}</span>
                        </template>
                    </van-cell>

                    <van-cell>
                        <template #title>
                            <span style="color: #aaa;">联系方式:</span>
                        </template>
                        <template #default>
                            <a :href="'tel:'+info.buyer_tel">{{info.buyer_tel}}</a>
                        </template>
                    </van-cell>

                </van-cell-group>
            </div>
        </div>


        <van-cell-group style="margin-top: 15px;">
            <van-field v-model="info.ticket_code" placeholder="请输入核销码" :readOnly="readOnly">
                <template #button>
                    <van-button size="small" type="primary" v-if="info == '' " @click="searchCode">查询</van-button>
                    <van-button size="small" type="primary" v-else @click="checkCode">核销</van-button>
                </template>
            </van-field>
        </van-cell-group>

    </van-pull-refresh>
    {include file="store/_tabbar" /}

</div>
<script>
    new Vue({
        el: "#app",
        data(){return{
            ticket_code: '',
            isLoading: false,
            readOnly: false,
            info: [],
            order_id: '{$Think.get.order_id}',
            active:0,
            lefttext:'我的',
        }},
        created(){
            let that = this
            if (this.order_id){
                this.getOrderDetail()
            }
        },
        methods:{
            // 扫一扫调用
            scanQRCode() {
                let that = this
                wx.ready(function () {
                    wx.scanQRCode({
                        needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                    });
                })
            },
            // 获取订单详情
            getOrderDetail() {
                let that = this
                axios.post("{:url('order/getOrderDetail')}",{
                    order_id: that.order_id,
                    ticket_code: that.info.ticket_code
                }).then(function (res) {
                    let ret = res.data
                    if (ret.code == 0){
                        vant.Notify({ type: 'success', message: '查询成功，检查信息无误后进行核销操作',duration: 1000 });
                        that.info = ret.data
                        that.readOnly = true
                    }else{
                        vant.Notify({ type: 'danger', message: ret.msg,duration: 1000 });
                    }
                })
            },
            searchCode(){
                let that = this
                if(that.info.ticket_code == '' || that.info.ticket_code == undefined){
                    vant.Notify({ type: 'danger', message: '请输入核销码' });
                }else{
                    that.getOrderDetail()
                }
            },
            // 核销
            checkCode(){
                let that = this

                if (that.info.order_status != 30){
                    vant.Notify({ type: 'danger', message: '当前订单已被核验' });
                }

                axios.post("{:url('order/checkOrderTicketCode')}",{
                        order_id: that.order_id,
                        ticket_code: that.info.ticket_code
                }).then(function (res) {
                    let ret = res.data
                    if (ret.code == 0){
                        vant.Notify({ type: 'success', message: ret.msg,duration:1000 });
                        that.info = []
                    }else{
                        vant.Notify({ type: 'danger', message: ret.msg,duration: 1000 });
                    }
                    that.readOnly = false
                })
            },
            onTabbarChange(index){
                let url = '';
                switch (index) {
                    case 0:url = "{:url('store/index')}";break;
                    case 1:url = "{:url('store/goods_index')}";break;
                    case 2:url = "{:url('store/order_index')}";break;
                    case 3:url = "{:url('store/info_index')}";break;
                }
                window.location.href = url;
            },
            jumpUser(){
                if (this.order_id == ''){
                    this.lefttext = "我的"
                    window.location.href = "{:url('user/index')}"
                }else{
                    this.lefttext = "返回"
                    window.location.back()
                }
            }
        },
    })
</script>
</body>
</html>
